<template>
    <!-- 对话框表单 -->
    <el-dialog
        custom-class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                <el-form-item label="用户">
                    <el-input :value="baTable.form.items!.nickname" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.before')">
                    <el-input :value="baTable.form.items!.before" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.money')">
                    <el-input :value="baTable.form.items!.money" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.after')">
                    <el-input :value="baTable.form.items!.after" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.ratio')">
                    <el-input :value="baTable.form.items!.ratio" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.receive')">
                    <el-input :value="baTable.form.items!.receive" readonly />
                </el-form-item>
                <el-form-item :label="t('withdrawal.remark')">
                    <el-input :value="baTable.form.items!.remark" readonly />
                </el-form-item>
                <FormItem v-if="ed == 0" :label="t('withdrawal.state')" type="radio" v-model="baTable.form.items!.state" prop="state" :data="{ content: { 0: t('withdrawal.state 0'), 1: t('withdrawal.state 1'), 2: t('withdrawal.state 2') } }" :input-attr="{ placeholder: t('Please select field', { field: t('withdrawal.state') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass
const ed = ref(0)

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    user_id: [buildValidatorData('required', t('withdrawal.user_id'))],
    before: [buildValidatorData('required', t('withdrawal.before'))],
    money: [buildValidatorData('required', t('withdrawal.money'))],
    after: [buildValidatorData('required', t('withdrawal.after'))],
    ratio: [buildValidatorData('required', t('withdrawal.ratio'))],
    receive: [buildValidatorData('required', t('withdrawal.receive'))],
    remark: [buildValidatorData('required', t('withdrawal.remark'))],
    state: [buildValidatorData('required', t('withdrawal.state'))],
})


watch(
    () => baTable.form.items,
    (newVal) => {
        if(newVal){
            ed.value = newVal!.ed
        }
    }
)

</script>

<style scoped lang="scss"></style>
